package panel.login;

import Base.FileOperation;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import frame.Home;
import frame.Login;
import function.LogIn;
import global.Const;

import global.PublicClass;
import listen.MyListener;
import other.DButton;
import panel.common.PasswordField;
import panel.common.TextField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class LogInPanel extends JPanel implements ActionListener {
    private TextField username = new TextField("用户名");
    private PasswordField password = new PasswordField("密  码");

    public LogInPanel() {
        init();
    }

    public void init() {
        // 面板设置
        setBounds(0, 0, Const.PANEL_WIDTH, Const.PANEL_HEIGHT);
        setLayout(null);
        setOpaque(false);
        setVisible(false);

        // 文本框
        username.setBounds(20, 320, 500, 64);
        username.setMaxLength(16);
        add(username);
        password.setBounds(20, 480, 500, 64);
        password.setMaxLength(16);
        add(password);

        // 登录按钮
        DButton logIn = new DButton("登 录");
        logIn.setBounds(70, 800, 400, 64);
        logIn.addActionListener(this);
        add(logIn);

        // 注册按钮
        DButton signUp = new DButton("注册");
        signUp.setFont(new Font("宋体", Font.BOLD, 24));
        signUp.setBounds(440, 10, 90, 32);
        signUp.addActionListener(this);
        add(signUp);

    }

    public void open() {
        setVisible(true);
    }

    public void close() {
        setVisible(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.print(e.getActionCommand() + "\n");
        switch (e.getActionCommand()) {
            case "登 录":
                if (username.getText().equals("")) {
                    Login.messageDialog.open("登录失败", "用户名不能为空");
                    break;
                } else if (password.getText().equals("")) {
                    Login.messageDialog.open("登录失败", "密码不能为空");
                    break;
                }
                // 处理登录请求
                login();
                break;
            case "注册":
                close();
                Login.signUpPanel.open();
                break;
        }
    }

    // 处理登录请求
    private void login() {
        SwingWorker worker = new SwingWorker<JSONObject, Void>() {
            @Override
            protected JSONObject doInBackground() throws Exception {
                Login.loadingDialog.open(); // 打开加载窗口
                return LogIn.get(username.getText(), password.getText()); // 返回服务器的值
            }

            @Override
            protected void done() {
                try {
                    JSONObject reserve = get();
                    if (reserve.getInteger("code") == 0) {// 登录成功
                        // 保存用户名和密码到本地
                        JSONObject user = JSONObject.parseObject(String.format("{\"username\":\"%s\",\"password\":\"%s\"}",
                                username.getText(), password.getText()));
                        String data = user.toString(JSONWriter.Feature.PrettyFormat);
                        FileOperation.write("src/main/resources/data/user/log", data);
                        System.out.print("已将用户信息保存到文件\n" + data);
                        PublicClass.home.open();
                        System.out.print("登录成功\n");
                        PublicClass.login.close();
                    } else {// 登录失败
                        Login.messageDialog.open(reserve.getString("title"), reserve.getString("message"));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    Login.loadingDialog.close();
                }
            }
        };
        worker.execute();
    }

}
